package com.lyj.util;

import com.lyj.pojo.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class PermissionInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.println("自定义拦截");
        User loginUser = (User) httpServletRequest.getSession().getAttribute("loginUser");
        System.out.println(loginUser);
        if (loginUser == null){
//            httpServletRequest.getRequestDispatcher("/login.jsp").forward(httpServletRequest,httpServletResponse);
            httpServletResponse.sendRedirect("/HRM/toLogin");
//            httpServletResponse.sendError(500,"没有登录，无法访问");
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
//        System.out.println("拦截成功后回调");
//        User loginUser = (User) httpServletRequest.getSession().getAttribute("loginUser");
//        if (loginUser != null){
//            System.out.println("已经登录");
//        }else {
//            System.out.println("没有登录，跳转登录页面");
//            httpServletRequest.getRequestDispatcher("login.jsp").forward(httpServletRequest,httpServletResponse);
//        }
    }
}
